package com.zhongketech.sunflower;


import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.database.AppInfo;
import com.zhongketech.sunflower.database.ApplicationData;
import com.zhongketech.sunflower.database.Data;
import com.zhongketech.sunflower.database.PhoneNumberData;
import com.zhongketech.sunflower.filter.AppManager;
import com.zhongketech.sunflower.util.Constants;
import com.zhongketech.sunflower.util.Root;
import com.zhongketech.sunflower.util.SystemUiHider;
import com.zhongketech.sunflower.util.Utilities;
import com.zhongketech.sunflower.view.LogUpdate;
import com.zhongketech.sunflower.view.PhoneNumList;
import com.zhongketech.sunflower.view.UninstallList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;



public class LockActivity extends BaseActivity {

	private final static String TAG = "LockActivity";
	private static Context context = null;

	private MyHandler myHandler = null;

	private static int sleepTimeCount = 0; 
	private static int sleepTime = 500;

	/**
	 * View init
	 */
	private EditText editText = null;
	private Button button = null;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.lockphone);

		//custom
		myHandler = new MyHandler();
		initView();

	}

	private void initView()
	{
		context = this;
		editText = (EditText) findViewById(R.id.numEdit);
		button = (Button) findViewById(R.id.numButton);
		button.setOnClickListener(onClickListener);
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {  
			// TODO Auto-generated method stub
			final String input = editText.getText().toString();

			//如果输入的字符已点分割，则认为输入的是包名，否则，认为输入的应用名字
			if(null != input && input.matches("[0-9]+"))
			{
				Utilities.logCat(TAG, "lock num: "+input, Constants.LOG_INFO);
				sleepTimeCount = Integer.parseInt(input) * 1000;
				Intent intent = new Intent();  
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
				intent.setClass(LockActivity.this, UnUsePhoneActivity.class);  
				startActivity(intent);
				thread.start();
			}
			else
			{
				Toast.makeText(getApplicationContext(), "非法输入，请输入纯数字",
						Toast.LENGTH_SHORT).show();
				return;
			}

		}
	};

	private String getRunPackage()
	{
		ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);  
		ComponentName cn = am.getRunningTasks(1).get(0).topActivity;  
		return  cn.getPackageName(); 
	}

	Thread thread = new Thread(new Runnable()  
	{  
		int count = 1;
		@Override  
		public void run()  
		{   
			boolean isStop = true;


			while(isStop)
			{
				// TODO Auto-generated method stub  
				try {
					Thread.sleep(sleepTime);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Utilities.logCat(TAG, "sleepTime * count "+sleepTime*count+"  sleepTimeCount: "+sleepTimeCount,Constants.LOG_ONLYSHELL);

				if(sleepTime * count >= sleepTimeCount)
				{
					isStop = false;
				}

				count++;

				String packages = getRunPackage();
				Utilities.logCat(TAG, "run packages "+packages,Constants.LOG_ONLYSHELL);
				if(null != packages)
				{
					if(!("com.zhongketech.sunflower".equals(packages)))
					{
						List<String> cmdList = new ArrayList<String>();

						cmdList.clear();
						cmdList.add("am start -a com.zhongketech.sunflower.UnUsePhoneActivity.launch");
						try {
							Root.doCmds(cmdList);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} 

						Utilities.killProcess(packages, context);
					}
				}
			}
		}  
	}); 

	/**
	 * 数据 更新操作
	 */
	static class MyHandler extends Handler
	{


		@Override
		public void handleMessage(Message msg) 
		{
			Utilities.logCat(TAG, "msg.what "+msg.what+" msg.arg1: "+msg.arg1+" msg.arg2: "+msg.arg2,Constants.LOG_ONLYSHELL);
		}

	}
}
